package com.tjvib.util;

import android.util.Log;

import java.util.Locale;


/**
 * 管理Log的工具类：可通过设置mCurrentLevel，控制Log输出级别。
 * 项目上线时应将mCurrentLevel设置为LEVEL_NONE，禁止Log输出。
 */
public class LogUtil {
    //日志输出级别NONE
    public static final int LEVEL_NONE = 0;
    //日志输出级别E
    public static final int LEVEL_ERROR = 1;
    //日志输出级别W
    public static final int LEVEL_WARN = 2;
    //日志输出级别I
    public static final int LEVEL_INFO = 3;
    //日志输出级别D
    public static final int LEVEL_DEBUG = 4;
    //日志输出级别V
    public static final int LEVEL_VERBOSE = 5;

    private static int mCurrentLevel = LEVEL_VERBOSE;
    // 日志截断长度，避免打印不全
    private static int segmentSize = 3 * 1024;

    private static String getTag() {
        StackTraceElement[] trace = new Throwable().fillInStackTrace()
                .getStackTrace();
        String callingClass = "";
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtil.class)) {
                callingClass = trace[i].getClassName();
                callingClass = callingClass.substring(callingClass
                        .lastIndexOf('.') + 1);
                break;
            }
        }
        return callingClass;
    }

    private static String buildMessage(String msg) {
        StackTraceElement[] trace = new Throwable().fillInStackTrace()
                .getStackTrace();
        String caller = "";
        for (int i = 2; i < trace.length; i++) {
            Class<?> clazz = trace[i].getClass();
            if (!clazz.equals(LogUtil.class)) {
                caller = trace[i].getMethodName();
                break;
            }
        }
        return String.format(Locale.US, "[%d] %s: %s", Thread.currentThread()
                .getId(), caller, msg);
    }

    public static void v(String msg) {
        long length = msg.length();
        if (length <= segmentSize) {
            LogUtil.vHelper(getTag(), buildMessage(msg));
        } else {
            while (msg.length() > segmentSize) {
                String logContent = msg.substring(0, segmentSize);
                msg = msg.replace(logContent, "");
                LogUtil.vHelper(getTag(), buildMessage(logContent));
            }
            LogUtil.vHelper(getTag(), buildMessage(msg));
        }
    }

    public static void d(String msg) {
        long length = msg.length();
        if (length <= segmentSize) {
            LogUtil.dHelper(getTag(), buildMessage(msg));
        } else {
            while (msg.length() > segmentSize) {
                String logContent = msg.substring(0, segmentSize);
                msg = msg.replace(logContent, "");
                LogUtil.dHelper(getTag(), buildMessage(logContent));
            }
            LogUtil.dHelper(getTag(), buildMessage(msg));
        }
    }

    public static void i(String msg) {
        long length = msg.length();
        if (length <= segmentSize) {
            LogUtil.iHelper(getTag(), buildMessage(msg));
        } else {
            while (msg.length() > segmentSize) {
                String logContent = msg.substring(0, segmentSize);
                msg = msg.replace(logContent, "");
                LogUtil.iHelper(getTag(), buildMessage(logContent));
            }
            LogUtil.iHelper(getTag(), buildMessage(msg));
        }
    }

    public static void w(String msg) {
        long length = msg.length();
        if (length <= segmentSize) {
            LogUtil.wHelper(getTag(), buildMessage(msg));
        } else {
            while (msg.length() > segmentSize) {
                String logContent = msg.substring(0, segmentSize);
                msg = msg.replace(logContent, "");
                LogUtil.wHelper(getTag(), buildMessage(logContent));
            }
            LogUtil.wHelper(getTag(), buildMessage(msg));
        }
    }

    public static void e(String msg) {
        long length = msg.length();
        if (length <= segmentSize) {
            LogUtil.eHelper(getTag(), buildMessage(msg));
        } else {
            while (msg.length() > segmentSize) {
                String logContent = msg.substring(0, segmentSize);
                msg = msg.replace(logContent, "");
                LogUtil.eHelper(getTag(), buildMessage(logContent));
            }
            LogUtil.eHelper(getTag(), buildMessage(msg));
        }
    }

    public static void vHelper(String tag, String mess) {
        if (mCurrentLevel >= LEVEL_VERBOSE) {
            Log.v(tag, mess);
        }
    }

    public static void dHelper(String tag, String mess) {
        if (mCurrentLevel >= LEVEL_DEBUG) {
            Log.d(tag, mess);
        }
    }

    public static void iHelper(String tag, String mess) {
        if (mCurrentLevel >= LEVEL_INFO) {
            Log.i(tag, mess);
        }
    }

    public static void wHelper(String tag, String mess) {
        if (mCurrentLevel >= LEVEL_WARN) {
            Log.w(tag, mess);
        }
    }

    public static void eHelper(String tag, String mess) {
        if (mCurrentLevel >= LEVEL_ERROR) {
            Log.e(tag, mess);
        }
    }

    /**
     * 以级别为 w 的形式输出Throwable
     */
    public static void w(Throwable tr) {
        if (mCurrentLevel >= LEVEL_WARN) {
            Log.w(getTag(), "", tr);
        }
    }

    /**
     * 以级别为 w 的形式输出LOG信息和Throwable
     */
    public static void w(String msg, Throwable tr) {
        if (mCurrentLevel >= LEVEL_WARN && null != msg) {
            Log.w(getTag(), msg, tr);
        }
    }


    /**
     * 以级别为 e 的形式输出Throwable
     */
    public static void e(Throwable tr) {
        if (mCurrentLevel >= LEVEL_ERROR) {
            Log.e(getTag(), "", tr);
        }
    }

    /**
     * 以级别为 e 的形式输出LOG信息和Throwable
     */
    public static void e(String msg, Throwable tr) {
        if (mCurrentLevel >= LEVEL_ERROR && null != msg) {
            Log.e(getTag(), msg, tr);
        }
    }
}
